#include "wageeditdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QMessageBox>
#include <QSqlError>
#include <QDebug>

WageEditDialog::WageEditDialog(QWidget *parent)
    : QDialog(parent), m_isEditMode(false), m_wageId(-1)
{
    setWindowTitle("工价信息");
    setFixedSize(400, 300);
    setupUI();
    loadToyData();
    
    // 连接验证信号
    connect(m_toyNameCombo, &QComboBox::currentTextChanged, this, &WageEditDialog::validateInput);
    
    // 初始验证
    validateInput();
}

void WageEditDialog::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout();
    
    // 玩具名称下拉框
    m_toyNameCombo = new QComboBox();
    m_toyNameCombo->setEditable(false);
    formLayout->addRow("玩具名称:", m_toyNameCombo);
    
    // 总工价 (精确到厘)
    m_totalWageSpin = new QDoubleSpinBox();
    m_totalWageSpin->setRange(0, 999999.999);
    m_totalWageSpin->setDecimals(3);
    m_totalWageSpin->setValue(0.000);
    formLayout->addRow("总工价:", m_totalWageSpin);
    
    // 员工工价 (精确到厘)
    m_employeeWageSpin = new QDoubleSpinBox();
    m_employeeWageSpin->setRange(0, 999999.999);
    m_employeeWageSpin->setDecimals(3);
    m_employeeWageSpin->setValue(0.000);
    formLayout->addRow("员工工价:", m_employeeWageSpin);
    
    // 利润 (精确到厘)
    m_profitSpin = new QDoubleSpinBox();
    m_profitSpin->setRange(0, 999999.999);
    m_profitSpin->setDecimals(3);
    m_profitSpin->setValue(0.000);
    formLayout->addRow("利润:", m_profitSpin);
    
    mainLayout->addLayout(formLayout);
    
    // 对话框按钮
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    mainLayout->addWidget(m_buttonBox);
    
    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

void WageEditDialog::loadToyData()
{
    m_toyNameCombo->clear();
    m_toyMap.clear();
    
    QSqlQuery query("SELECT id, name FROM toys ORDER BY name");
    while (query.next()) {
        int id = query.value(0).toInt();
        QString name = query.value(1).toString();
        
        m_toyNameCombo->addItem(name);
        m_toyMap[name] = id;
    }
}

void WageEditDialog::setEditMode(bool isEdit, int wageId)
{
    m_isEditMode = isEdit;
    m_wageId = wageId;
    
    if (isEdit) {
        setWindowTitle("编辑工价信息");
    } else {
        setWindowTitle("添加工价信息");
    }
}

void WageEditDialog::setWageData(const QString &toyName, double totalWage, double employeeWage, double profit)
{
    m_toyNameCombo->setCurrentText(toyName);
    m_totalWageSpin->setValue(totalWage);
    m_employeeWageSpin->setValue(employeeWage);
    m_profitSpin->setValue(profit);
}

QString WageEditDialog::getToyName() const
{
    return m_toyNameCombo->currentText();
}

double WageEditDialog::getTotalWage() const
{
    return m_totalWageSpin->value();
}

double WageEditDialog::getEmployeeWage() const
{
    return m_employeeWageSpin->value();
}

double WageEditDialog::getProfit() const
{
    return m_profitSpin->value();
}

int WageEditDialog::getWageId() const
{
    return m_wageId;
}

void WageEditDialog::validateInput()
{
    bool valid = !m_toyNameCombo->currentText().isEmpty();
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}